using UnityEngine;
using System.Collections;

public class Cabin : MonoBehaviour {
	
	#region Variables
	private static Vector3 location;
	public static Vector3 Location{
		get {return location;}
	}
	
	private bool visible;
	private static int isVisible;
	public static int IsVisible{
		get{return isVisible;}
		set{isVisible = value;}
	}
	
	private float lastTime;
	private static float nextTime;
	public static float NextTime{
		get{return nextTime;}
		set{nextTime=value;}
	}
	
	private Collider childCollider;
	public Collider ChildCollider{
		get{return childCollider;}
	}
	
	#endregion
	
	// Use this for initialization
	void Start () {
		location = Vector3.zero;
		visible = true;
		lastTime = 0;
		nextTime = 0;
		childCollider = GetComponentInChildren<Collider>();
	}
	
	// Update is called once per frame
	void Update () {
		
		childCollider = GetComponentInChildren<Collider>();
		
		if(visible){isVisible=1;}
		else if(!visible){isVisible=0;}
		//Counter Rotates the cars based on speed of the wheels rotation
		transform.Rotate(WheelRotation.Rotation*-1); 
		location = transform.position;
		if(visible || isVisible == 1){
			renderer.enabled=true;
			
		}else if(!visible || isVisible == 0){
			
			renderer.enabled=false;
			collider.enabled=childCollider.enabled;
			
			
		}
		
		if(lastTime  < WheelRotation.CouterSecond || lastTime==0){
			visible=true;
			collider.enabled=childCollider.enabled;
			
			isVisible=1;
		}
		lastTime = nextTime;
	}
	
	void OnCollisionEnter(Collision collision){	
	
		if(collision.transform.tag=="bomb"){
			//Destroy(collision.gameObject);
			//Destroy(this.gameObject);
			
			
			collider.enabled=childCollider.enabled;
			visible=false;
			lastTime = WheelRotation.CouterSecond+5;
			nextTime = lastTime;
			
		}
	}


		

	
}
